﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;
using System.Globalization;

namespace _4T
{
    public partial class CMS_ProfileInfoEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["p"] != null)
            {
                if (!IsPostBack)
                {
                    LoadProfileInfo(Request.QueryString["p"].ToString());
                    hdfProfileCode.Value = Request.QueryString["p"].ToString();
                }
            }
        }

        private void LoadProfileInfo(string profileCode)
        {
            ProfileInfoBLL infoBLL = new ProfileInfoBLL();
            CMS_ProfileInfo info = infoBLL.GetLastProfileInfoByProfileCode(profileCode);
            if (info != null)
            {
                // Load patient info
                PatientInfoBLL patientBLL = new PatientInfoBLL();
                PatientInfo patient = patientBLL.GetPatientInfoByProfileInfoCode(info.ProfileInfoCode);
                if (patient != null)
                {
                    txtPatientName.Text = patient.PatientName;
                    txtDOB.Text = patient.Birthday.ToShortDateString();
                    ddlGender.SelectedValue = patient.Gender;
                    txtIDNumber.Text = patient.IDNumber;
                    txtPermAddr.Text = patient.PermanentAddress;
                    txtTempAddr.Text = patient.TemporaryAddress;
                    txtMobileNumber.Text = patient.MobileNumber;
                    txtPhoneNumber.Text = patient.PhoneNumber;
                    txtRepresentativeName.Text = patient.EmergencyRepresentative;
                    txtRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                    txtLocalContactName.Text = patient.LocalContactPoint;
                    txtLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                    txtMedicalStaffName.Text = patient.LocalMedicalStaff;
                    txtMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                    txtMedicalFacility.Text = patient.MedicalFacility;
                    txtNote.Text = patient.Note;
                }

                // Load medical info
                MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
                MedicalInfo medical = medicalBLL.GetMedicalInfoByProfileInfoCode(info.ProfileInfoCode);
                if (medical != null)
                {
                    txtDiagnosis.Text = medical.Diagnosis;
                    txtMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                    txtTreatmentMethod.Text = medical.TreatmentMethod;
                    txtSituation.Text = medical.Situation;
                    txtSupport.Text = medical.Support;
                }


                // Load info supplier info
                InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
                InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfoByProfileInfoCode(info.ProfileInfoCode);
                if (supplier != null)
                {
                    txtInfoSupplierName.Text = supplier.SupplierName;
                    txtInfoSupplierPhone.Text = supplier.PhoneNumber;
                    //txtPersonalCode.Text = supplier.PersonalCode;
                    txtEmail.Text = supplier.Email;
                }

                txtProposeDate.Text = info.CreateDate;

                // Load photo, video
                ProfileStageLink stage = new ProfileStageLinkBLL().GetProfileStageLinkByProfileInfoCodeAndStage(info.ProfileInfoCode, 1);
                if (stage != null)
                {
                    if (!string.IsNullOrEmpty(stage.PhotoLink))
                    {
                        if (stage.PhotoLink.IndexOf(';') < 0)
                        {
                            ltrImage.Text = "<img src=\"" + stage.PhotoLink + "\"/>";
                        }
                        else
                        {
                            string[] photoLinks = stage.PhotoLink.Split(';');
                            for (int i = 0; i < photoLinks.Length; i++)
                            {
                                ltrImage.Text += "<img src=\"" + photoLinks[i] + "\"/><br/>";
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(stage.VideoLink))
                    {
                        if (stage.PhotoLink.IndexOf(';') < 0)
                        {
                            Page.ClientScript.RegisterStartupScript(GetType(), "playvideo", "playvideo('" + stage.VideoLink + "');", true);
                        }
                        else
                        {
                            string[] videoLinks = stage.VideoLink.Split(';');
                            Page.ClientScript.RegisterStartupScript(GetType(), "playvideo", "playvideo('" + videoLinks[0] + "');", true);
                        }
                    }

                    txtDescription.Text = stage.Description;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            PatientInfo patient = new PatientInfo(txtPatientName.Text, Convert.ToDateTime(txtDOB.Text),
                ddlGender.SelectedValue, txtIDNumber.Text, txtPermAddr.Text, txtTempAddr.Text,
                txtMobileNumber.Text, txtPhoneNumber.Text, txtRepresentativeName.Text,
                txtRepresentativePhone.Text, txtLocalContactName.Text, txtLocalContactPhone.Text,
                txtMedicalStaffName.Text, txtMedicalStaffPhone.Text, txtMedicalFacility.Text,
                txtNote.Text);
            MedicalInfo medical = new MedicalInfo(txtDiagnosis.Text, Convert.ToDateTime(txtMedicalTime.Text),
                txtTreatmentMethod.Text, txtSituation.Text, txtSupport.Text, null);
            string supplierCode = "guestsoncamedia";
            //InfoSupplierInfo supplier = new InfoSupplierInfo(txtInfoSupplierName.Text,
                //txtInfoSupplierPhone.Text, supplierCode, txtEmail.Text, null);
            DateTime? proposeDate = null;
            if (!string.IsNullOrEmpty(txtProposeDate.Text)) proposeDate = Convert.ToDateTime(txtProposeDate.Text, CultureInfo.CreateSpecificCulture("vi-VI"));
            ProfileInfo profileInfo = new ProfileInfo(hdfProfileCode.Value);
            Profile profile = new Profile();
            profile.ProfileInfo = profileInfo;
            profile.PatientInfo = patient;
            profile.MedicalInfo = medical;
            //profile.InfoSupplierInfo = supplier;
            ProfileStageLink stageLink = new ProfileStageLinkBLL().GetProfileStageLinkByProfileCodeAndStage(hdfProfileCode.Value, 1);
            ProfileStatus status = new ProfileStatusBLL().GetProfileStatusByProfileCodeWithPureData(hdfProfileCode.Value);
            ProfileBLL profileBLL = new ProfileBLL();
            bool result = profileBLL.UpdateProfileInfoWithMedia(profile, stageLink, status.Proposer,supplierCode, proposeDate, txtDescription.Text);
            //bool result = profileBLL.InsertProfileInfo(profile);
            if (result) Response.Redirect("Default.aspx");
        }
    }
}